Skip to content

refactor: deduplicate accept loop, slowlog, pubsub in ember-server - #98

Merged
kacy merged 3 commits into
mainfrom
refactor/server-dedup
Feb 12, 2026
Merged

refactor: deduplicate accept loop, slowlog, pubsub in ember-server#98
kacy merged 3 commits into
mainfrom
refactor/server-dedup

Conversation

@kacy

@kacy kacy commented Feb 12, 2026

Copy link
Copy Markdown
Owner

summary

  • extracted accept_connection, tls_handshake, on_connection_done, and drain_connections helpers from server.rs, reducing the 4 accept-loop arms from ~25 lines each to ~8 lines
  • extracted lock_or_clear and lock_inner mutex helpers in slowlog.rs, replacing 4 inline lock-with-poison-recovery blocks
  • extracted subscribe_to and unsubscribe_from in pubsub.rs, deduplicating subscribe/psubscribe and unsubscribe/punsubscribe

what was tested

  • cargo clippy -p ember-server -- -D warnings — clean
  • cargo test -p ember-server — all 43 tests pass
  • verified behavioral equivalence: each helper does exactly what the inline code did, with no hidden control flow changes

design considerations

the accept_connection helper intentionally does not handle protected mode rejection — that requires writing to the owned stream, which the helper doesn't own. keeping the 3-line protected mode check in the caller preserves clarity about who owns the stream at each point.

kacy added 3 commits February 12, 2026 10:34
…rver.rs

run() and run_concurrent() duplicated ~200 lines of identical accept
logic across 4 select arms. extracted three helpers:

- accept_connection: sets nodelay, acquires semaphore, updates metrics
- tls_handshake: TLS handshake with 10s timeout
- on_connection_done: decrements active connections, records metrics
- drain_connections: shared shutdown drain with 30s timeout

each select arm now reads as a short pipeline instead of a wall of
inline bookkeeping.
the lock-with-poison-recovery pattern appeared 4 times with two
variants (clear-on-poison for writes vs recover-silently for reads).
extracted lock_or_clear() and lock_inner() — each call site is now
a single line.
subscribe/psubscribe and unsubscribe/punsubscribe differed only in
which DashMap they operated on. extracted subscribe_to() and
unsubscribe_from() — each public method is now a one-liner delegating
to the shared helper with the appropriate map.
@kacy
kacy merged commit edff6c7 into main Feb 12, 2026
6 of 7 checks passed
@kacy
kacy deleted the refactor/server-dedup branch February 12, 2026 15:51
kacy added a commit that referenced this pull request Feb 19, 2026
* refactor: extract accept-connection and tls-handshake helpers from server.rs

run() and run_concurrent() duplicated ~200 lines of identical accept
logic across 4 select arms. extracted three helpers:

- accept_connection: sets nodelay, acquires semaphore, updates metrics
- tls_handshake: TLS handshake with 10s timeout
- on_connection_done: decrements active connections, records metrics
- drain_connections: shared shutdown drain with 30s timeout

each select arm now reads as a short pipeline instead of a wall of
inline bookkeeping.

* refactor: extract slowlog mutex helpers

the lock-with-poison-recovery pattern appeared 4 times with two
variants (clear-on-poison for writes vs recover-silently for reads).
extracted lock_or_clear() and lock_inner() — each call site is now
a single line.

* refactor: deduplicate pubsub subscribe/unsubscribe

subscribe/psubscribe and unsubscribe/punsubscribe differed only in
which DashMap they operated on. extracted subscribe_to() and
unsubscribe_from() — each public method is now a one-liner delegating
to the shared helper with the appropriate map.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant